package com.engine.modules.speapi.controller;

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.engine.common.baseEntity.jsonResponse.JsonResponseEntity;
import com.engine.common.constant.CommonConstant;
import com.engine.common.util.TokenVerifyUtil.AuthRedisUtil;
import com.engine.common.util.TokenVerifyUtil.TokenVerifyUtil;
import com.engine.init.Globle;
import com.engine.modules.speapi.mapper.SpecialGisMapper;
import com.engine.modules.vector.mapper.VectorSourceMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiSort;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

@RestController
@Api(tags = "6-项目专用的接口")
@CrossOrigin
@RequestMapping("/Special/Gis")
@ApiSort(value = 6)
@Slf4j
public class SpecialGisController {
    @Autowired
    AuthRedisUtil authRedisUtil;
    @Autowired
    VectorSourceMapper vectorSourceMapper;

    @Autowired
    SpecialGisMapper specialGisMapper;

    @ApiOperation(value = "多geojson查询", notes = "通过Geometry（此Geometry是geojson中的数据格式），" +
            "并按照约定模式计算统计项，返回混合类型geojson,tables为空则会使用模式的表")
    @PostMapping(value = "/selectMultiTypeGeojsonByGeometry/{token}")
    public String selectMultiTypeGeojsonByGeometry(@RequestBody String geometry,
                                                   @RequestParam(value = "tables", required = false, defaultValue = "") String tables,
                                                   @RequestParam(value = "limit_area", required = false, defaultValue = "10000000000.0") Double limit_area,
                                                   @PathVariable("token") String token,
                                                   HttpServletResponse response) {
        try {
            String access = TokenVerifyUtil.tokenVerify(token);
            if (tables.equals(""))
                tables = Globle.TableNames;
            Double d = vectorSourceMapper.selectShapeAreaByGeometry(geometry);
            if (d > limit_area) {
                JsonResponseEntity jsonResponseEntity = new JsonResponseEntity(false, CommonConstant.SC_BAD_QUERY, "查询面积过大", response);
                return jsonResponseEntity.getJsonString();
            }
            String geojson = specialGisMapper.selectMultiTypeGeojsonByGeometry(tables, geometry, access);
            return geojson;
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            JsonResponseEntity jsonResponseEntity = new JsonResponseEntity(false, CommonConstant.SC_BK_ERROR, "查询出错，e：" + e.getMessage(), response);
            return jsonResponseEntity.getJsonString();
        }
    }

    @ApiOperation(value = "多数据统计查询", notes = "通过Geometry，并按照约定模式计算统计项，返回带统计信息json数据")
    @PostMapping(value = "/selectStatisticalByGeometry/{token}")
    public String selectStatisticalByGeometry(@RequestBody String geometry,
                                              @RequestParam(value = "rasterNames", required = false, defaultValue = "") String rasterNames,
                                              @RequestParam(value = "statisticalNames", required = false, defaultValue = "") String statisticalNames,
                                              @RequestParam(value = "limit_area", required = false, defaultValue = "10000000000.0") Double limit_area,
                                              @PathVariable("token") String token,
                                              HttpServletResponse response) {
        try {
            String access = TokenVerifyUtil.tokenVerify(token);
            if (rasterNames.equals(""))
                rasterNames = Globle.Rasters;
            if (statisticalNames.equals(""))
                statisticalNames = Globle.StatisticalNames;
            Double d = vectorSourceMapper.selectShapeAreaByGeometry(geometry);
            if (d > limit_area) {
                JsonResponseEntity jsonResponseEntity = new JsonResponseEntity(false, CommonConstant.SC_BAD_QUERY, "查询面积过大", response);
                return jsonResponseEntity.getJsonString();
            }
            List<Map<String, Object>> rsList = specialGisMapper.selectStatisticalByGeometry(statisticalNames, rasterNames, geometry, access);
            JsonResponseEntity jsonResponseEntity = new JsonResponseEntity(true, CommonConstant.SC_OK_200, "查询成功", rsList, 1, response);
            return jsonResponseEntity.getJsonString(SerializerFeature.WriteMapNullValue);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            JsonResponseEntity jsonResponseEntity = new JsonResponseEntity(false, CommonConstant.SC_BK_ERROR, "查询出错，e：" + e.getMessage(), response);
            return jsonResponseEntity.getJsonString();
        }
    }


    @ApiOperation(value = "getLocation", notes = "获取行政区划列表，若输入pac则获取该地区下级的列表")
    @GetMapping(value = "/getLocation/{pac}/{token}")
    public String getLocation(@PathVariable("pac") String pac,
                              @PathVariable("token") String token, HttpServletResponse response) {
        try {
            String access = TokenVerifyUtil.tokenVerify(token);
            List<Map<String, Object>> rsList = specialGisMapper.getLocation(pac);
            JsonResponseEntity jsonResponseEntity = new JsonResponseEntity(true, CommonConstant.SC_OK_200, "获取成功", rsList, null, response);
            return jsonResponseEntity.getJsonString();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            JsonResponseEntity jsonResponseEntity = new JsonResponseEntity(false, CommonConstant.SC_BK_ERROR, "查询出错，e：" + e.getMessage(), response);
            return jsonResponseEntity.getJsonString();
        }
    }
}
